#include <ne.h>
#include <graphics/BMPImageReader.h>
#include <graphics/PNGImageReader.h>
#include <system/GlutApplication.h>

int    Width;               /* Width of window */
int    Height;              /* Height of window */
ne::ImagePtr imageBMP;
ne::ImagePtr imagePNG;
float v = -1;

ne::Engine *pEngine = 0;
ne::GlutApplication *pApplication;

void keyboard(unsigned char key, int x, int y);
void redraw(void);
void resize(int width, int height);
void init();

int main(int argc, char *argv[]) /* I - Command-line arguments */
{
    pApplication = new ne::GlutApplication();//(argc, argv);
    glutReshapeFunc(resize);
    glutDisplayFunc(redraw);
    glutKeyboardFunc(keyboard);
    //glutFullScreen();

    pEngine = pApplication->engine;
    if (!pEngine || !pEngine->isReady())
    {
        delete pApplication;
        return 0;
    }

    init();
    
    glutMainLoop();

    delete pApplication;

    return 0;
}

void init()
{
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    ne::BMPImageReader bmpReader;
    imageBMP = bmpReader.loadImage("../image/logo.bmp");

    ne::PNGImageReader pngReader;
    imagePNG = pngReader.loadImage("../image/logo.png");
}

void keyboard(unsigned char key, int x, int y)
{
    if (key == 'u' || key == 'U')
    {
    }
    else if (key == 'j' || key == 'J')
    {
    }
    else if (key == 'r' || key == 'R')
    {
    }
    else if (key == 's' || key == 'S')
    {
    }
    else if (key == 0x1b)
    {
        exit(0);
    }
}

void redraw(void)
{
    glShadeModel(GL_SMOOTH);

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);

    glColor4f(1.0, 0.0, 0.0, 0.75);
    
    glPushMatrix();
        glTranslatef(0, 0, 0);

        v = (v + 0.01);//float(1000.0-(rand()%2000))/1000;
        if (v >= 1) v = -1;
        glRasterPos2f(v, 0);
        glDrawPixels(imageBMP->getWidth(), imageBMP->getHeight(), imageBMP->getFormat(), imageBMP->getPixelType(), imageBMP->getData());

        glRasterPos2f(0, 0);
        glDrawPixels(imagePNG->getWidth(), imagePNG->getHeight(), imagePNG->getFormat(), imagePNG->getPixelType(), imagePNG->getData());

    glPopMatrix();
    glutSwapBuffers();
    glutPostRedisplay();
    //Sleep(10);
}

void resize(int width, int height)
{
    /* Save the new width and height */
    Width  = width;
    Height = height;

    /* Reset the viewport... */
    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
}

